(() => {
  let h_id = location.search.split("?")[1];
  console.log(h_id, "????????");
  let ele = {};
  ele.top_img = $("#top_img");
  ele.reward = $("#reward");
  ele.shangqi = $("#shangqi");
  ele.img_btn = $("#img_btn");
  ele.listcontent = $("#listcontent");
  ele.qifu = $("#qifu");
  ele.qifuzhi = $("#qifuzhi");
  ele.jinduP = $("#jinduP");
  ele.btn2 = $("#btn2");
  ele.qi = $("#qi");
  ele.haohuazhi = $("#haohuazhi");
  ele.shuaxin = $("#shuaxin");
  ele.modal_illustrates = $("#modal-illustrates");
  ele.chahao2 = $("#chahao2");
  ele.btn_que3 = $("#btn_que3");
  ele.img1_btn = $("#img1_btn");
  ele.zhaocai = $("#zhaocai");
  ele.top_list = $("#top_list");
  ele.btn_top = $("#btn_top");
  ele.img_top = $("#img_top");
  ele.yiwancheng = $("#yiwancheng");
  ele.manyizhi = $("#manyizhi");
  let list = [];
  let detail = {};

  // 刘欣 引导 2023-6-22 15:20:23
  ele.top_img.onclick = () => {
	let ugid = guide.getUsersGuideId();
    if (~[131].indexOf(ugid)) guide.hideCentury_131();
    history.back();
  };

  // 奖励领取
  ele.reward.onclick = () => {
    location.href = "../page/wealReward.html";
  };
  // todo招财猫的动图
  ele.zhaocai.innerHTML = `<img class="catimg" src="http://img.qxgs.cc/tiantianlaishouzu/img/low_Ent_ousing/zhao${h_id}.gif" alt="" />
  `;
  // 领取详情
  ele.shangqi.onclick = () => {
    location.href = "../page/catdetail.html";
  };
  // 进入排行榜
  ele.img_btn.onclick = () => {
    location.href = "../page/joyrank.html";
  };
  // todo点击祈福
  ele.btn2.onclick = () => {
    qifu();
  };
  // todo点击关闭弹出层
  ele.btn_que3.onclick = () => {
    closeMdl(ele.modal_illustrates);
  };
  ele.chahao2.onclick = () => {
    closeMdl(ele.modal_illustrates);
  };
  // todo/点击玩法介绍打开弹出层
  ele.img1_btn.onclick = () => {
    openMdl(ele.modal_illustrates);
  };
  //!亲求数据
  AsyncList();
  function AsyncList(data) {
    let token = getCookie("token");
    ajax({
      method: "get",
      url: API_PATH.BRINGLIST,
      header: {
        Authorization: token,
      },
      data: {
        h_id: h_id,
      },
      success: (res) => {
        list = res.data.list;
        detail = res.data.detail;
		if(res.data.detail.type == 1) {
			ele.manyizhi.innerHTML = 'x' + res.data.detail.gold_money
		} else {
			ele.manyizhi.innerHTML = 'x' + res.data.detail.gold_money
		}
        listXuan(detail);
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }
  // !点击最上面的宝箱在这里调用接口
  ele.btn_top.onclick = () => {
    diyige();
  };
  // 渲染数据
  function listXuan(detail) {
	  // console.log(detail.type && h_id == 2);
    if (detail.type && h_id == 2) {
      ele.yiwancheng.innerHTML = detail.myz_num;
      if (detail.is_ling == 1) {
		ele.btn_top.onclick = null
        ele.btn_top.classList.add("gray");
        ele.btn_top.innerHTML = "已领取";
      }
      openMdl(ele.top_list);
    } else {
      openMdl(ele.top_list);
    }
    // !渲染倒计时
    let time = setInterval(() => {
      if (detail.next_time * 1000 <= +new Date()) {
        clearInterval(time);
      }
      ele.shuaxin.innerHTML = `距离下次任务刷新还有${getCountTime(
        detail.next_time
      )}`;
    }, 1000);
    let newList = list
      .map((item) => {
        return `   <div class="list">
    <p class="list1">${item.describe}(<span>${
          item.record ? item.record.num : "0"
        }</span>/${item.num})</p>
    <div class="list2">
      <img src="http://img.qxgs.cc/tiantianlaishouzu/img/wealth/qifu.png" alt="" />
      <span>x${item.money}</span>
    </div>
    <button class="list3 ${
      item.record && item.record.state == 10 ? "gray" : ""
    }" id="listNum" data-state="${
          item.record ? item.record.state : ""
        }" data-id="${item.id}">${
          item.record && item.record.state == 10 ? "已领取" : "领取"
        }</button>
  </div>`;
      })
      .join(" ");
    ele.listcontent.innerHTML = newList;

    // !渲染祈福卷数量
    ele.qifu.innerHTML = `x${detail.qf_num}`;
    // !进度条
    ele.qifuzhi.innerHTML = `${detail.now_qualify}/${detail.qualify}`;
    // !渲染期数
    ele.qi.innerHTML = `${detail.qi}期`;
    // !豪华值渲染
    ele.haohuazhi.innerHTML = detail.my_qualify;
    // 计算高度
    let width = +detail.now_qualify / detail.qualify;
    let wid = width * 100;
    ele.jinduP.style.width = wid + "%";
    // !给领取绑定接口
    let listNumAll = document.querySelectorAll("#listNum");
    listNumAll.forEach((item) => {
      item.onclick = () => {
        // if (item.dataset.state) {
        //   // 如果存在就调用领取方法
        getLingJiang(item.dataset.id);
        // } else {
        // getLingQu(item.dataset.id);
        // }
      };
    });
  }
  // !祈福接口
  function qifu() {
    let token = getCookie("token");
    ajax({
      method: "post",
      url: API_PATH.PRAY,
      header: {
        Authorization: token,
      },
      success: (res) => {
        openTips(res.msg);
        AsyncList();
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }
  // !领取任务接口
  function getLingQu(data) {
    let token = getCookie("token");
    ajax({
      method: "post",
      url: API_PATH.LINGQU,
      header: {
        Authorization: token,
      },
      data: {
        id: data,
      },
      success: (res) => {
        openTips(res.msg);
        if (res.code == 1) {
          AsyncList();
        }
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }
  // todo第一个任务
  function diyige(data) {
    let token = getCookie("token");
    ajax({
      method: "get",
      url: API_PATH.ZHAOCAIJNBAO,
      header: {
        Authorization: token,
      },
      success: (res) => {
        openTips(res.msg);
        AsyncList();
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }
  // !领取接口
  function getLingJiang(data) {
    let token = getCookie("token");
    ajax({
      method: "post",
      url: API_PATH.REWARD,
      header: {
        Authorization: token,
      },
      data: {
        id: data,
      },
      success: (res) => {
        openTips(res.msg);
        if (res.code == 1) {
          AsyncList();
        }
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }
})();
